package com.miku.sys.dao;

import java.util.List;

import org.hibernate.Session;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;
import com.miku.common.BaseDaoImpl;
import com.miku.db.sys.entity.SysRole;
import com.miku.db.sys.entity.SysUser;
import com.miku.utils.SQLHelper;

@Transactional
@Repository
@SuppressWarnings("unchecked")
public class SysUserDaoImpl extends BaseDaoImpl<SysUser> implements SysUserDaoI {

	public SysUserDaoImpl() {
		super(SysUser.class);
	}
	
	public List<SysRole> listByUserID(int userID) {
		Session s = getSession();
		List<Integer> list = s.createSQLQuery("select role_id from SysUserRole where user_id=" + userID).list();
		if(list.size() > 0){
			return s.createQuery("from SysRole where id in(:ids)").setParameterList("ids", list).list();
		}
		return Lists.newArrayList();
	}

	@Override
	public SysUser findByLoginName(String name) {
		return (SysUser) getSession().createQuery("from SysUser where loginName=:n").setString("n", name).uniqueResult();
	}

	/**
	 * 修改昵称
	 */
	@Override
	public void mergeNickName(String nickname,int id) {
		
		sqlQuery("update sys_user set nickname=:nickname where id=:id")
		.setString("nickname", nickname)
		.setInteger("id", id)
		.executeUpdate();
	}

	//验证注册邮箱是否存在
	@Override
	public Integer isExistsEmail(String email) {
		return SQLHelper.intVal(sqlQuery("select count(*) from sys_user where LoginName=:email")
				.setString("email", email)
				.uniqueResult()
				);
	}

	@Override
	public int countUser() {
		return SQLHelper.intVal(sqlQuery("select count(*) from sys_user")
				.uniqueResult()
				);
	}

	@Override
	public List<SysUser> getListUser(int firstResult, int maxResults) {
		return hqlQuery("from SysUser order by id")
					.setFirstResult(firstResult)
					.setMaxResults(maxResults)
					.list();
	}
	
	@Override
	public void modifyAllowLogin(String state ,int userID) {
		sqlQuery("update sys_user set allowloign=:state where id=:userID")
		.setString("state", state)
		.setInteger("userID", userID)
		.executeUpdate();
	}

	@Override
	public int countNoLoginUser(String state) {
		return SQLHelper.intVal(sqlQuery("select count(*) from sys_user where allowloign=:state")
				.setString("state", state)
				.uniqueResult()
				);
	}

	@Override
	public List<SysUser> listNoLoginUser(int firstResult, int maxResults, String state) {
		return hqlQuery("from SysUser where allowloign=:state order by id")
				.setString("state", state)
				.setFirstResult(firstResult)
				.setMaxResults(maxResults)
				.list();
	}

	@Override
	public List<SysUser> listOnlineUser(int firstResult, int maxResults, List<Integer> ids) {
		return hqlQuery("from SysUser where id in (:ids) order by id")
				.setParameterList("ids", ids)
				.setFirstResult(firstResult)
				.setMaxResults(maxResults)
				.list();
	}

	@Override
	public SysUser findByNickname(String name) {
		return (SysUser) getSession().createQuery("from SysUser where nickname=:n").setString("n", name).uniqueResult();
	}
}
